/************************************************************************
 * FilePath: /src/dict_client.c
 * 创 建 人: George Gao
 * Gitee: https://gitee.com/EmbeddedGeorge/Online_Dictionary
 * 创建日期: 2020年02月27日 15:05:34
 * 文件描述: 
 * 版 本 号: 1.0
 * 修改日期: 2020年02月28日 16:51:14
 * 修改记录: 初建
 * *********************************************************************/

#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define Register  1
#define Login     2
#define Query     3
#define History   4

//定义通信双方的信息结构体
typedef struct 
{
    int type;
    char name[32];
    char data[256];
}MSG;

int do_register(int sockfd, MSG *msg)
{
    msg->type = Register;
    printf("Input user name:");
    scanf("%s", msg->name);
    getchar();
    printf("Input password:");
    scanf("%s", msg->data);
    getchar();

    if((send(sockfd, msg, sizeof(MSG), 0)) < 0){
        printf("send error\r\n");
        return -1;
    }
    if((recv(sockfd, msg, sizeof(MSG), 0)) < 0){
        printf("recv error\r\n");
        return -1;
    }
    else{
        //Ok! or usr already exist.
        printf("recv msg.data:%s\r\n", msg->data);
    }
    
    return 0;
}

int do_login(int sockfd, MSG *msg)
{
    msg->type = Login;
    printf("Input user name:");
    scanf("%s", msg->name);
    getchar();
    printf("Input password:");
    scanf("%s", msg->data);
    getchar();

    if((send(sockfd, msg, sizeof(MSG), 0)) < 0){
        printf("send error\r\n");
        return -1;
    }
    if((recv(sockfd, msg, sizeof(MSG), 0)) < 0){
        printf("recv error\r\n");
        return -1;
    }
    else{
        //Ok! or usr already exist.
        if((strncmp(msg->data, "OK", 3)) == 0){
            printf("Login Suscess!\r\n");
            return 1;
        }
        else{
            printf("%s\r\n", msg->data);
            return 0;
        }
    }

    return 0;
}

int do_query(int sockfd, MSG *msg)
{
    msg->type = Query;
    puts("-----------------");
    while(1)
    {
        printf("Input word:");
        scanf("%s", msg->data);
        //客户端输入 “#” 返回上级菜单
        if((strncmp(msg->data, "#", 1)) == 0){
            break;
        }
        
        //将要查询的单词，发送给服务器
        if((send(sockfd, msg, sizeof(MSG), 0)) < 0){
            printf("send error\r\n");
            return -1;
        }
        //等待接收服务器传回的单词信息
        if((recv(sockfd, msg, sizeof(MSG), 0)) < 0){
            printf("recv error\r\n");
            return -1;
        }
        else{
            printf("%s\r\n", msg->data);
        }
    }
    return 0;
}

int do_history(int sockfd, MSG *msg)
{
    msg->type = History;

    //将要查询的单词，发送给服务器
    if((send(sockfd, msg, sizeof(MSG), 0)) < 0){
        printf("send error\r\n");
        return -1;
    }
    //等待接收服务器传回的单词信息
    if((recv(sockfd, msg, sizeof(MSG), 0)) < 0){
        printf("recv error\r\n");
        return -1;
    }
    else{
        printf("%s\r\n", msg->data);
    }
    
    return 0;
}

// ./server 192.168.11.192 10000
int main(int argc, char *argv[])
{
    int sockfd;
    struct sockaddr_in serveraddr;
    int cmd;
    MSG msg;
    if(argc != 3){
        printf("Usage:%s serverip port.\r\n", argv[0]);
        return -1;
    }
    
    if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
        perror("socket error\r\n");
        exit(-1);
    }
    bzero(&serveraddr, sizeof(serveraddr));
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_addr.s_addr = inet_addr(argv[1]);
    serveraddr.sin_port = htons(atoi(argv[2]));

    if((connect(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr))) < 0){
        perror("connect error\r\n");
        exit(-1);
    }

    while(1)
    {
        // system("clear");
        printf("**********************************************\r\n");
        printf("* 1. register       2. login       3. quite  *\r\n");
        printf("**********************************************\r\n");
        printf("Please choose:");
        scanf("%d", &cmd);
        getchar(); //去掉垃圾字符
        
        switch(cmd)  //根据用户输入，进行相应的操作
        {
            case 1:do_register(sockfd, &msg);break;
            case 2:if(do_login(sockfd, &msg) == 1){
                        goto next;
                    };
                    break;
            case 3:close(sockfd);system("clear");exit(0);break;
            default:system("clear");printf("Invalid Input! Try again!\r\n");break;
        }
    }
    
next:
    while(1)
    {
        // system("clear");
        printf("**********************************************\r\n");
        printf("* 1. query_word  2. history_record  3. quite *\r\n");
        printf("**********************************************\r\n");
        printf("Please choose:");
        scanf("%d", &cmd);
        getchar(); //去掉垃圾字符
        
        switch(cmd)
        {
            case 1:do_query(sockfd, &msg);break;
            case 2:do_history(sockfd, &msg);break;
            case 3:close(sockfd);system("clear");exit(0);break;
            default:system("clear");printf("Invalid Input! Try again!\r\n");break;
        }
    }

    return 0;
}

/***************************** End Of File *****************************/

